<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="ES6-JS 一些新的API1https:&#x2F;&#x2F;juejin.cn&#x2F;post&#x2F;7016520448204603423    数组合并并去重​        先使用扩展运算符进行转化为一个数组，然后再使用set方式去重。 12345let arr1 &#x3D; [1, 3, 5];let arr2 &#x3D; [1, 6, 12];let arr &#x3D; [...new Set([...arr1, ...arr2])];">
<meta property="og:type" content="article">
<meta property="og:title" content="ES6随笔1">
<meta property="og:url" content="http://tallgy.gitee.io/2021/11/12/JavaScript/ES6%E9%9A%8F%E7%AC%941/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="ES6-JS 一些新的API1https:&#x2F;&#x2F;juejin.cn&#x2F;post&#x2F;7016520448204603423    数组合并并去重​        先使用扩展运算符进行转化为一个数组，然后再使用set方式去重。 12345let arr1 &#x3D; [1, 3, 5];let arr2 &#x3D; [1, 6, 12];let arr &#x3D; [...new Set([...arr1, ...arr2])];">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-11-12T06:10:35.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.323Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="随笔">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="ES6">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://tallgy.gitee.io/2021/11/12/JavaScript/ES6%E9%9A%8F%E7%AC%941/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2021/11/12/JavaScript/ES6%E9%9A%8F%E7%AC%941/","path":"2021/11/12/JavaScript/ES6随笔1/","title":"ES6随笔1"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>ES6随笔1 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#ES6-JS-%E4%B8%80%E4%BA%9B%E6%96%B0%E7%9A%84API"><span class="nav-number">1.</span> <span class="nav-text">ES6-JS 一些新的API</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E5%90%88%E5%B9%B6%E5%B9%B6%E5%8E%BB%E9%87%8D"><span class="nav-number">1.1.</span> <span class="nav-text">数组合并并去重</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AF%B9%E8%B1%A1%E5%90%88%E5%B9%B6"><span class="nav-number">1.2.</span> <span class="nav-text">对象合并</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8B%BC%E6%8E%A5%E5%8A%A0%E5%88%A4%E6%96%AD"><span class="nav-number">1.3.</span> <span class="nav-text">字符串拼接加判断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8-includes-%E8%BF%9B%E8%A1%8C%E6%88%96%E5%88%A4%E6%96%AD"><span class="nav-number">1.4.</span> <span class="nav-text">使用 includes 进行或判断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8-find-%E5%92%8C-filter-%E8%BF%9B%E8%A1%8C%E6%9F%A5%E6%89%BE"><span class="nav-number">1.5.</span> <span class="nav-text">使用 find 和 filter 进行查找</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%89%81%E5%B9%B3%E5%8C%96%E6%95%B0%E7%BB%84"><span class="nav-number">1.6.</span> <span class="nav-text">扁平化数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%A9%BA%E5%80%BC%E5%90%88%E5%B9%B6%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">1.7.</span> <span class="nav-text">空值合并运算符 ??</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%82%E6%AD%A5%E5%87%BD%E6%95%B0"><span class="nav-number">1.8.</span> <span class="nav-text">异步函数</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2021/11/12/JavaScript/ES6%E9%9A%8F%E7%AC%941/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ES6随笔1
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-11-12 14:10:35" itemprop="dateCreated datePublished" datetime="2021-11-12T14:10:35+08:00">2021-11-12</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/%E9%9A%8F%E7%AC%94/" itemprop="url" rel="index"><span itemprop="name">随笔</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="ES6-JS-一些新的API"><a href="#ES6-JS-一些新的API" class="headerlink" title="ES6-JS 一些新的API"></a>ES6-JS 一些新的API</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://juejin.cn/post/7016520448204603423</span><br></pre></td></tr></table></figure>



<h2 id="数组合并并去重"><a href="#数组合并并去重" class="headerlink" title="数组合并并去重"></a>数组合并并去重</h2><p>​        先使用扩展运算符进行转化为一个数组，然后再使用set方式去重。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">let arr1 = [1, 3, 5];</span><br><span class="line">let arr2 = [1, 6, 12];</span><br><span class="line"></span><br><span class="line">let arr = [...new Set([...arr1, ...arr2])];</span><br><span class="line">console.log(arr);</span><br></pre></td></tr></table></figure>



<h2 id="对象合并"><a href="#对象合并" class="headerlink" title="对象合并"></a>对象合并</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">let obj = &#123;...obj1, ...obj2&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="字符串拼接加判断"><a href="#字符串拼接加判断" class="headerlink" title="字符串拼接加判断"></a>字符串拼接加判断</h2><p>使用模板字符串，模板字符串内部的${}，可以放入任意的JavaScript表达式</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">const result = `$&#123;name&#125;$&#123;score &gt; 60 ? &#x27;的考试及格&#x27; : &#x27;的考试不及格&#x27;&#125;`;</span><br></pre></td></tr></table></figure>



<h2 id="使用-includes-进行或判断"><a href="#使用-includes-进行或判断" class="headerlink" title="使用 includes 进行或判断"></a>使用 includes 进行或判断</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const arr = [1, 2, 3];</span><br><span class="line">if (arr.includes(type)) &#123;&#125;</span><br><span class="line"></span><br><span class="line">就等于了</span><br><span class="line">if (type === 1 || type===2 || type===3) &#123;&#125;</span><br></pre></td></tr></table></figure>



<h2 id="使用-find-和-filter-进行查找"><a href="#使用-find-和-filter-进行查找" class="headerlink" title="使用 find 和 filter 进行查找"></a>使用 find 和 filter 进行查找</h2><ul>
<li>find 的特点是只查找第一个满足的</li>
<li>filter是将所有满足的作为一个新的数组返回。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">arr.filter(v =&gt; &#123;</span><br><span class="line">	return v === 3;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">arr.find(v =&gt; &#123;</span><br><span class="line">	return v === 3;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h2 id="扁平化数组"><a href="#扁平化数组" class="headerlink" title="扁平化数组"></a>扁平化数组</h2><ul>
<li>Object.values，这个代表了取出对象的属性值，然后作为一个数组。</li>
<li>flat 是一个扁平化数组的方法，Infinity作用是将很多层扁平化为一层。具体的可以取MDN进行查看。</li>
<li>注意： <code>flat</code> 方法不支持IE浏览器</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const deps = &#123;</span><br><span class="line">	key: [],</span><br><span class="line">	key1: []</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">let member = Object.values(deps).flat(Infinity);</span><br></pre></td></tr></table></figure>



<h2 id="空值合并运算符"><a href="#空值合并运算符" class="headerlink" title="空值合并运算符 ??"></a>空值合并运算符 ??</h2><ul>
<li><code>??</code> 代表了如果 左侧的变量是 null或者是undefined时，那么将会返回右侧的操作数。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">let a ?? &#x27;&#x27;</span><br></pre></td></tr></table></figure>



<h2 id="异步函数"><a href="#异步函数" class="headerlink" title="异步函数"></a>异步函数</h2><ul>
<li>就是Promise.all 和 Promise.race 的使用</li>
<li>以及可以使用 async 函数。</li>
<li>避免生成回调地狱。</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E9%9A%8F%E7%AC%94/" rel="tag"># 随笔</a>
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
              <a href="/tags/ES6/" rel="tag"># ES6</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/11/11/JavaScript/ES6/JavaScript-ES6%E8%A7%A3%E6%9E%84/" rel="prev" title="JavaScript-ES6解构">
                  <i class="fa fa-chevron-left"></i> JavaScript-ES6解构
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/11/12/JavaScript/%E9%9A%8F%E7%AC%94/%E5%87%BD%E6%95%B0%E6%9F%AF%E9%87%8C%E5%8C%96/" rel="next" title="函数柯里化">
                  函数柯里化 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
